package com.morn.weibo.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class ListBaseAdapter<T> extends BaseAdapter {

	protected List<T> mObjects;

	protected List<T> selecteds;

	private boolean mNotifyOnChange = true;

	private final Object mLock = new Object();

	public ListBaseAdapter() {
		this.mObjects = new ArrayList<T>();
	}

	public void setData(List<T> data) {
		synchronized (mLock) {
			clear();
			if (data != null) {
				addAll(data);
			}
		}
	}

	public void clear() {
		synchronized (mLock) {
			mObjects.clear();
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void add(T object) {
		synchronized (mLock) {
			mObjects.add(object);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void addAll(Collection<? extends T> collection) {
		synchronized (mLock) {
			mObjects.addAll(collection);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void insert(T object, int index) {
		synchronized (mLock) {
			mObjects.add(index, object);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void intser(Collection<? extends T> collection, int index) {
		synchronized (mLock) {
			mObjects.addAll(index, collection);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void insertToHead(T object) {
		synchronized (mLock) {
			mObjects.add(0, object);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void insertToBottom(T object) {
		synchronized (mLock) {
			mObjects.add(mObjects.size(), object);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void insertToHead(Collection<? extends T> collection) {
		synchronized (mLock) {
			mObjects.addAll(0, collection);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void insertToBottom(Collection<? extends T> collection) {
		synchronized (mLock) {
			mObjects.addAll(mObjects.size(), collection);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void remove(T object) {
		synchronized (mLock) {
			mObjects.remove(object);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void removeAll(Collection<? extends T> collection) {
		synchronized (mLock) {
			mObjects.removeAll(collection);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	public void sort(Comparator<? super T> comparator) {
		synchronized (mLock) {
			Collections.sort(mObjects, comparator);
		}
		if (mNotifyOnChange)
			notifyDataSetChanged();
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		mNotifyOnChange = true;
	}

	public void setNotifyOnChange(boolean notifyOnChange) {
		mNotifyOnChange = notifyOnChange;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mObjects.size();
	}

	@Override
	public T getItem(int position) {
		// TODO Auto-generated method stub
		return mObjects.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public abstract View getView(int position, View convertView,
			ViewGroup parent);

}
